package com.gistmap.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.gistmap.dao.ArticleDao;
import com.gistmap.dto.Friend;
import com.gistmap.dto.MessageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gistmap.bean.User;
import com.gistmap.dao.UserDao;
import com.gistmap.service.UserService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService
{
	@Autowired
	private UserDao userDao;
	@Autowired
	private ArticleDao articleDao;
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public List<User> getAll()
	{
		return userDao.getAll();
	}

	public User getByUserName(String username)
	{
		return userDao.getByUserName(username);
	}

	public int register(User user)
	{
		int i  = userDao.register(user);
		return i;
	}

	public int nameRepeat(String username)
	{
		return userDao.nameRepeat(username);
	}

	public Friend get(Integer id) {
		return userDao.get(id);
	}

	public void saveAvatar(String fileName,Integer uid) {
		userDao.saveAvatar(fileName,uid);
	}

	public void update(User user) {
		userDao.update(user);
	}

	public User getById(Integer id) {
		return userDao.getById(id);
	}

	public List<Integer> curFocusStatus(Integer uid) {
		return userDao.curFocusStatus(uid);
	}

	public List<Integer> curFocusMeStatus(Integer uid) {
		return userDao.curFocusMeStatus(uid);
	}
	@Transactional
	public void saveFriend(Integer curId, Integer careId,Map map) {

		try {
			//1 保存关系到t_friend
			userDao.saveFriend(curId,careId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			//2 为被关注人添加一条message
			userDao.addMessage(careId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			//3 为这个关注事件记录
			userDao.saveAction(map);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/*
		同上
	 */
	@Transactional
	public void closeFriend(Integer curId, Integer careId, Map map) {

		try {
			userDao.closeFriend(curId,careId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			userDao.addMessage(careId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			userDao.saveAction(map);
		} catch (Exception e) {
			e.printStackTrace();
		}


	}

	/*
		得到所有有我相关的记录
	 */
	public List<MessageDto> getRelated(Integer uid) {
		return userDao.getRelated(uid);
	}

	/*
		清空用户表所有message
	 */
	@Transactional
	public void closeMessage(Integer uid) {
		//1 清除用户表的message
		userDao.clearMessage(uid);
		//2 将记录条flag设0，即已读
		userDao.clearFlag(uid);
	}

	/*
		关注人数量
	 */
	public Integer getCareCount(Integer id) {
		return userDao.getCareCount(id);
	}

	/*
		粉丝数量
	 */
	public Integer getFansCount(Integer id) {
		return userDao.getFansCount(id);
	}

	/*
		朋友列表
	 */
	public List<Friend> getFriend(Integer uid) {
		return userDao.getFriend(uid);
	}

	public List<Friend> getFans(Integer uid) {
		return userDao.getFans(uid);
	}




}
